{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ab7f5d42",
   "metadata": {},
   "source": [
    "# 列表练习 & 初始字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8b6763a",
   "metadata": {},
   "source": [
    "## 数字、字符串和对象"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0fbffbed",
   "metadata": {},
   "source": [
    "###### 数字：变量会取所赋的那个值的类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0056f80b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import random\n",
    "wait_time = random.randint(1,60)\n",
    "wait_time"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4725ca1",
   "metadata": {},
   "source": [
    "###### 字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "79b966bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'bottles'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "word = \"bottles\"\n",
    "word"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "898e2155",
   "metadata": {},
   "source": [
    "###### 对象：python中所有的一切都是对象，而且所有的对象都可以赋给变量。对象可以有状态和行为。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08ea91a8",
   "metadata": {},
   "source": [
    "## 知识点_课本练习"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dbecb6c5",
   "metadata": {},
   "source": [
    "###### 目标： \"Don't panic\" ==> \"on tap\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19ecbfd2",
   "metadata": {},
   "source": [
    "###  方法一： 使用list的方法完成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bd75d0f9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)  # list()可以将 str  to(转)  list\n",
    "print(phrase)\n",
    "print(plist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1ff3d1b",
   "metadata": {},
   "source": [
    "* 步骤：首先，删除后面四个元素 a n i c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bc43639c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a']\n"
     ]
    }
   ],
   "source": [
    "for i in range(4):\n",
    "    plist.pop()\n",
    "print(plist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3aec948",
   "metadata": {},
   "source": [
    "* 步骤：第二步， 删除 D  ‘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7afd01ba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['o', 'n', \"'\", 't', ' ', 'p', 'a']\n",
      "['o', 'n', 't', ' ', 'p', 'a']\n"
     ]
    }
   ],
   "source": [
    "plist.remove('D')\n",
    "print(plist)\n",
    "plist.pop(2)\n",
    "print(plist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57ef7e83",
   "metadata": {},
   "source": [
    "* 步骤：第三步，调换a和p的顺序   ;数学案例： 计算 （(a+b)*(c+d)）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3fc78234",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['o', 'n', 't', ' ', 'a', 'p']\n"
     ]
    }
   ],
   "source": [
    "plist.extend([plist.pop(),plist.pop()])\n",
    "# ['o','n','t',' '][ a    ,   p     ]\n",
    "print(plist)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fea9205d",
   "metadata": {},
   "source": [
    "* 步骤：第四步，数据插入  实现：’t‘ 和’ ‘ 的位置转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c7285cba",
   "metadata": {},
   "outputs": [],
   "source": [
    "plist.insert(2,plist.pop(3))\n",
    "print(plist)         # 这一步已经删除了2这个位置的值，所以打印要加上\n",
    "print(plist.pop(2))  # 1，代表删除的内容的值 2，他删除了plist的这个值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d15c2a97",
   "metadata": {},
   "source": [
    "* 步骤：第五步，list to str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b5aeea06",
   "metadata": {},
   "outputs": [],
   "source": [
    "new_phrase = ''.join(plist)\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4180da56",
   "metadata": {},
   "source": [
    "* 问：plist 改变了原先的状态么？\n",
    "* 答：列表的方法会改变列表本身，如果不想打破或者想保留原先数据的内容，\n",
    "    请先用copy()复制数据"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36c6cff1",
   "metadata": {},
   "source": [
    "### 方法二： 使用切片的方法实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "fecb6c17",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "on\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)  # list() 可以将 str  to(转)  list\n",
    "print(phrase)\n",
    "print(plist)\n",
    "\n",
    "\n",
    "on_str = ''.join(plist[1:3])\n",
    "print(on_str)  # 直接打印1和2这两个位置的值\n",
    "new_phrase = on_str+ ''.join(plist[5]+plist[4]+plist[7]+plist[6])\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf989936",
   "metadata": {},
   "source": [
    "### 别的列表课本练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8b9f7d82",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels: Milliways\n",
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input(\"Provide a word to search for vowels: \")\n",
    "found = []\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        if letter not in found:\n",
    "            found.append(letter)\n",
    "for vowel in found:\n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80b28c07",
   "metadata": {},
   "source": [
    "## 元组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3012d8d7",
   "metadata": {},
   "source": [
    "###### 四个内置数据结构：列表、元组、字典、集合  \n",
    "###### 用途：用来保存任何对象集合"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c07f0239",
   "metadata": {},
   "source": [
    "###### 有序的不可变对象集合"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88e83ad7",
   "metadata": {},
   "source": [
    "###### 元组是一个不可变的列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "970af797",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(12345, 54321, 'hello!')\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "t = 12345, 54321, 'hello!'\n",
    "print(t)\n",
    "print(type(t))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "28dee9d8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "time.struct_time(tm_year=2021, tm_mon=10, tm_mday=24, tm_hour=23, tm_min=45, tm_sec=57, tm_wday=6, tm_yday=297, tm_isdst=0)\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "print(time.localtime())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "845c77b0",
   "metadata": {},
   "source": [
    "## 课堂练习"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0bb3947",
   "metadata": {},
   "source": [
    "###### 自己写版本（很多地方需改进）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a14b84f8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的余额：1896\n",
      "------商品列表------\n",
      "['10001', '瑞幸咖啡', '15']\n",
      "['10002', 'iphone13 pro', '9999']\n",
      "['10003', '笔', '3']\n",
      "['10004', '奥特曼模型', '500']\n",
      "['10005', '联想笔记本电脑', '7000']\n",
      "\n",
      "请输入商品编号：10001\n",
      "['10001', '瑞幸咖啡', '15']\n",
      "您的余额为: 1881\n",
      "您的购买清单为： ['10001', '瑞幸咖啡', '15']\n",
      "请问还有需要购买的东西吗？不需要再次购买\n"
     ]
    }
   ],
   "source": [
    "# 购物车程序项目：\n",
    "# 要求：1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "# 　　  2、让用户输入商品编号进行商品的购买。\n",
    "# 　　  3、用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "# 　　  4、用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。\n",
    "\n",
    "商品 = [\n",
    "    [\"10001\", \"瑞幸咖啡\", \"15\"],\n",
    "    [\"10002\", \"iphone13 pro\", \"9999\"],\n",
    "    [\"10003\", \"笔\", \"3\"],\n",
    "    [\"10004\", \"奥特曼模型\", \"500\"],\n",
    "    [\"10005\", \"联想笔记本电脑\", \"7000\"]\n",
    "]\n",
    "需要再次购买 = [\"是\"]\n",
    "不需要再次购买 = [\"否\"]\n",
    "退出 = [\"退出购买页面，欢迎下次光临哦~\"]\n",
    "用户购物列表 = []\n",
    "\n",
    "支付宝余额 = input(\"请输入您的余额：\")\n",
    "info = '''------商品列表------\n",
    "%s\n",
    "%s\n",
    "%s\n",
    "%s\n",
    "%s\n",
    "'''%(商品[0], 商品[1], 商品[2], 商品[3], 商品[4])\n",
    "print(info)\n",
    "user_input_ID =input(\"请输入商品编号：\")\n",
    "for good in 商品:\n",
    "    print(good)\n",
    "    # 判断商品id是否正确\n",
    "    if user_input_ID == good[0]:\n",
    "        # 判断用户的余额是否够用\n",
    "        if int(good[2]) < int(支付宝余额):\n",
    "            # 把商品加进购物车里\n",
    "            支付宝余额 = int(支付宝余额)-int(good[2])\n",
    "            # 扣除支付宝余额\n",
    "            print(\"您的余额为:\", 支付宝余额)\n",
    "        if int(good[2]) > int(支付宝余额):\n",
    "            print(\"您的余额不足，请及时充值！\")\n",
    "        break\n",
    "\n",
    "    else:\n",
    "        print(\"您输入的商品信息有问题，请输入正确的商品编号！\")\n",
    "\n",
    "print(\"您的购买清单为：\", good)\n",
    "用户选择 = input(\"请问还有需要购买的东西吗？\")\n",
    "if 用户选择 == 不需要再次购买:\n",
    "    print(退出)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0601afb3",
   "metadata": {},
   "source": [
    "###### 老师上课操作完整版"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d042ae63",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的余额：88\n",
      "---------【商品列表】---------\n",
      "商品编号：0，商品名称：珍珠奶茶小杯，商品的价格：7 元\n",
      "商品编号：1，商品名称：大杯珍珠奶茶，商品的价格：8 元\n",
      "商品编号：2，商品名称：加大杯珍珠奶茶，商品的价格：9 元\n",
      "商品编号：3，商品名称：1L珍珠奶茶，商品的价格：12 元\n",
      "商品编号：4，商品名称：桶装珍珠奶茶，商品的价格：20 元\n",
      "请输入正确的商品编号(输入“q”可以退出购买)：4\n",
      "【桶装珍珠奶茶】 已经添加到您的购物车，您的余额还有 【68】 元\n",
      "---------【商品列表】---------\n",
      "商品编号：0，商品名称：珍珠奶茶小杯，商品的价格：7 元\n",
      "商品编号：1，商品名称：大杯珍珠奶茶，商品的价格：8 元\n",
      "商品编号：2，商品名称：加大杯珍珠奶茶，商品的价格：9 元\n",
      "商品编号：3，商品名称：1L珍珠奶茶，商品的价格：12 元\n",
      "商品编号：4，商品名称：桶装珍珠奶茶，商品的价格：20 元\n",
      "请输入正确的商品编号(输入“q”可以退出购买)：4\n",
      "【桶装珍珠奶茶】 已经添加到您的购物车，您的余额还有 【48】 元\n",
      "---------【商品列表】---------\n",
      "商品编号：0，商品名称：珍珠奶茶小杯，商品的价格：7 元\n",
      "商品编号：1，商品名称：大杯珍珠奶茶，商品的价格：8 元\n",
      "商品编号：2，商品名称：加大杯珍珠奶茶，商品的价格：9 元\n",
      "商品编号：3，商品名称：1L珍珠奶茶，商品的价格：12 元\n",
      "商品编号：4，商品名称：桶装珍珠奶茶，商品的价格：20 元\n",
      "请输入正确的商品编号(输入“q”可以退出购买)：4\n",
      "【桶装珍珠奶茶】 已经添加到您的购物车，您的余额还有 【28】 元\n",
      "---------【商品列表】---------\n",
      "商品编号：0，商品名称：珍珠奶茶小杯，商品的价格：7 元\n",
      "商品编号：1，商品名称：大杯珍珠奶茶，商品的价格：8 元\n",
      "商品编号：2，商品名称：加大杯珍珠奶茶，商品的价格：9 元\n",
      "商品编号：3，商品名称：1L珍珠奶茶，商品的价格：12 元\n",
      "商品编号：4，商品名称：桶装珍珠奶茶，商品的价格：20 元\n",
      "请输入正确的商品编号(输入“q”可以退出购买)：4\n",
      "【桶装珍珠奶茶】 已经添加到您的购物车，您的余额还有 【8】 元\n",
      "---------【商品列表】---------\n",
      "商品编号：0，商品名称：珍珠奶茶小杯，商品的价格：7 元\n",
      "商品编号：1，商品名称：大杯珍珠奶茶，商品的价格：8 元\n",
      "商品编号：2，商品名称：加大杯珍珠奶茶，商品的价格：9 元\n",
      "商品编号：3，商品名称：1L珍珠奶茶，商品的价格：12 元\n",
      "商品编号：4，商品名称：桶装珍珠奶茶，商品的价格：20 元\n",
      "请输入正确的商品编号(输入“q”可以退出购买)：4\n",
      "您的余额不够，请更换支付方式\n",
      "---------【商品列表】---------\n",
      "商品编号：0，商品名称：珍珠奶茶小杯，商品的价格：7 元\n",
      "商品编号：1，商品名称：大杯珍珠奶茶，商品的价格：8 元\n",
      "商品编号：2，商品名称：加大杯珍珠奶茶，商品的价格：9 元\n",
      "商品编号：3，商品名称：1L珍珠奶茶，商品的价格：12 元\n",
      "商品编号：4，商品名称：桶装珍珠奶茶，商品的价格：20 元\n",
      "请输入正确的商品编号(输入“q”可以退出购买)：q\n",
      "----------------- \n",
      " 您的购物车：[['桶装珍珠奶茶', 20], ['桶装珍珠奶茶', 20], ['桶装珍珠奶茶', 20], ['桶装珍珠奶茶', 20]]  \n",
      " 您的余额还有：8 元\n"
     ]
    }
   ],
   "source": [
    "# 1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "# 商品列表\n",
    "goods_list = [\n",
    "    # 商品1 商品名称，商品价格，商品编号...\n",
    "    # 商品模板 ： [商品名称,商品名称]\n",
    "    [\"珍珠奶茶小杯\",7],\n",
    "    [\"大杯珍珠奶茶\",8.5],\n",
    "    [\"加大杯珍珠奶茶\",9.5],\n",
    "    [\"1L珍珠奶茶\",12],\n",
    "    [\"桶装珍珠奶茶\",20],\n",
    "]\n",
    "\n",
    "# 不是所有的代码都可以一次性考虑全面！例如这里应该写一个用户商品列表\n",
    "user_shopping_cart = [] # append 可以新增列表\n",
    "\n",
    "# 用户输入： input\n",
    "Alipay = int(input(\"请输入您的余额：\"))\n",
    "\n",
    "# 打印商品列表 （规范打印，样式是先写的，然后再输入数据）\n",
    "# 先设计模板样式： 商品编号：xx，商品名称：xx，商品的价格：xx 元\n",
    "while True:\n",
    "    print(\"---------【商品列表】---------\")\n",
    "    for i in range(len(goods_list)):\n",
    "        # print(\"商品编号：%s，商品名称：%s，商品的价格：%d 元\"%(0,goods_list[0][0],goods_list[0][1]))\n",
    "        print(\"商品编号：%s，商品名称：%s，商品的价格：%d 元\"%(i,goods_list[i][0],goods_list[i][1]))\n",
    "\n",
    "    # 2、让用户输入商品编号进行商品的购买。\n",
    "    choice = input(\"请输入正确的商品编号(输入“q”可以退出购买)：\") # 用户输入可能不是数值，也可能是\n",
    "    if choice.isdigit():\n",
    "        # 用int转换数据类型为数值\n",
    "        choice = int(choice)\n",
    "        # 是正确的编号\n",
    "        if choice < len(goods_list):  # 商品的信息：goods_list[choice] ，商品名称：goods_list[choice][0]  商品价格:goods_list[choice][1]\n",
    "            # 1. 看一看用户钱够不够； Alipay VS goods_list[choice][1]\n",
    "            if Alipay < goods_list[choice][1]:\n",
    "                print(\"您的余额不够，请更换支付方式\")\n",
    "            # 2. 如果够的话，赶紧扣钱！把商品加到购物车\n",
    "            else:\n",
    "                Alipay = Alipay - goods_list[choice][1]\n",
    "                user_shopping_cart.append(goods_list[choice])\n",
    "                # 提示用户：本次购买成功\n",
    "                print(\"【%s】 已经添加到您的购物车，您的余额还有 【%d】 元\"%(goods_list[choice][0],Alipay))\n",
    "\n",
    "        # 不是正确的编号\n",
    "        else:\n",
    "            print(\"您输入的商品不存在，请输入正确的编号\")\n",
    "    elif choice == \"q\":\n",
    "        print(\"-----------------\",'\\n',\"您的购物车：%s \"%(user_shopping_cart),'\\n',\"您的余额还有：%d 元\"%(Alipay))\n",
    "        break\n",
    "        # 两种情况：\n",
    "        # 1. 按照我们的要求输入的文本，例如：输入q表示quit退出的意思\n",
    "        \n",
    "        # 2. 输入的不符合我们的要求的文本\n",
    "    else:\n",
    "        print(\"请输入正确的商品编号！！或者输入“q”退出\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b00b4023",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.0b3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
